package com.prs.crm.action.pm;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.pm.PmGroup;
import com.prs.crm.domain.pm.PmGroupMember;
import com.prs.crm.domain.pm.PmSpecialty;
import com.prs.crm.service.pm.PmGroupMemberService;
import com.prs.crm.service.pm.PmGroupService;
import com.prs.crm.service.pm.PmSpecialtyService;

public class PmGroupAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;

	private static final String key = "com.prs.crm.pm.groupkey";
	
	private PmGroup pmGroup;
	private PmGroupMember pmGroupMember;
	private PmGroupMemberService pmGroupMemberService;
	private PmSpecialtyService pmSpecialtyService;
	private PmGroupService pmGroupService;
	private PaginationSupport pagination;
	private Integer[] ids;
	
	private File upload;
	private String uploadFileName;
	private String costWork;
	
	private Collection<PmSpecialty> specialtys;
	private Collection<PmGroupMember> members;
	private TreeNode root;
	
	private boolean existStor;

	
	public boolean isExistStor() {
		return existStor;
	}

	public void setExistStor(boolean existStor) {
		this.existStor = existStor;
	}

	@Action("listPmGroup")
	public String listPmGroup() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PmGroup.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmGroupService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editPmGroup")
	public String editPmGroup() {
		specialtys = this.getPmSpecialtyService().getAllPmSpecialty();
		if (!existStor)
		{
			this.getStorage().remove(key);
			if (pmGroup!=null)
			{
				members = new ArrayList<PmGroupMember>();
				this.getStorage().save(key, members);
				if (pmGroup.getMembers()!=null)
					members.addAll(pmGroup.getMembers());
			}
		}
		if (getStorMembers()==null)
		{
			members = new ArrayList<PmGroupMember>();
			this.getStorage().save(key, members);
		}
//		if(pmGroupMember != null) {
//			pmGroup.addMember(pmGroupMember);
//		}
		
//		if(pmGroup != null) {
//			for(PmGroupMember member : pmGroup.getMembers()) {
//				members.add(member);
//			}
//		}

		return SUCCESS;
	}
	
	@Action(value="appendGroupMember",results={@Result(name="success",type="chain",location="editPmGroup"),
			@Result(name="error",type="chain",location="editPmGroup")})
	public String appendGroupMember() {
		this.setExistStor(true);
		if(!ToolKits.isNumeric(costWork)) {
			this.addActionError("工时成本应为整数型");
			return ERROR;
		}
		if(pmGroupMember.getMember() == null) {
			this.addActionError("请选择工作组成员");
			return ERROR;
		}
//		pmGroupMember.setId(members.size());
//		pmGroupMember.setCost(Double.valueOf(costWork));
//		members.add(pmGroupMember);
//		pmGroupMember.getMember().getResume().getName();
		
		if(pmGroup.getMembers() == null) {
			pmGroup.setMembers(new HashSet<PmGroupMember>());
		}
		pmGroupMember.setId(pmGroup.getMembers().size());
		pmGroupMember.setCost(Double.valueOf(costWork));
		if(pmGroup.getId() == null) {
			pmGroup.setId(-1);
		}
		pmGroup.addMember(pmGroupMember);
		members  = this.getStorMembers();
		if (members==null)
		{
			members = new ArrayList<PmGroupMember>();
			this.getStorage().save(key, members);
		}
		members.add(pmGroupMember);
		return SUCCESS;
	}
	
	@Action(value="removeGroupMember",results={@Result(name="success",type="chain",location="editPmGroup"),
			@Result(name="error",type="chain",location="editPmGroup")})
	public String removeGroupMember() {
		this.setExistStor(true);
		if(ids == null) {
			this.addActionError("请选择要删除的项目组成员");
			return ERROR;
		}
		if (getStorMembers()!=null)
		{
			members = getStorMembers();
			for(Integer id : ids) {
				for(PmGroupMember member : members) {
					if(id.equals(member.getId())) {
						members.remove(member);
						break;
					}
				}
			}
		}
		return SUCCESS;
	}
	
	@Action(value="submitPmGroup",results={@Result(name="success",type="redirect",location="listPmGroup"),
			@Result(name="error",type="chain",location="editPmGroup")})
	public String submitPmGroup() {
		this.setExistStor(true);
		members = getStorMembers();
		if(members==null || members.size() == 0) {
			this.addActionError("请添加工作组成员");
			return ERROR;
		}
		if(pmGroup.getName() == null || pmGroup.getName().trim().equals("")) {
			this.addActionError("请输入工作组名称");
			return ERROR;
		}
		if(pmGroup.getPrincipal() == null) {
			this.addActionError("请选择工作组负责人");
			return ERROR;
		}
		if (pmGroup.getMembers()!=null)
			pmGroup.getMembers().clear();
		this.getPmGroupService().submitPmGroup(pmGroup, upload, uploadFileName);
		
		for(PmGroupMember member : members) {
			member.setId(null);
			member.setGroup(pmGroup);
			pmGroupMemberService.save(member);
		}
		
		
		return SUCCESS;
	}
	
	@Action(value="loadWorkGroupTree",results={@Result(name="success",type="json",params={"root","root"})})
	public String loadWorkGroupTree() {
		root = new TreeNode();
		root.setId("-1");
		
		TreeNode node = new TreeNode();
		node.setId("0");
		node.setText("工作组选择");	
		
		Collection<PmGroup> pmGroups = this.getPmGroupService().getAllPmGroup();
		for(PmGroup group : pmGroups) {
			TreeNode groupNode = new TreeNode();
			groupNode.setId(group.getId().toString());
			String text = "项目组："+group.getName()+"  专业："+group.getSpecialty().getName() +"  负责人："+group.getPrincipal().getResume().getName();
			groupNode.setText(text);
			
			for(PmGroupMember member : group.getMembers()) {
				TreeNode memberNode = new TreeNode();
				memberNode.setId("member-"+member.getId().toString());
				memberNode.setText("组员："+member.getMember().getResume().getName()+"  工时成本："+member.getCost());
				
				groupNode.addChildNode(memberNode);
			}
			node.addChildNode(groupNode);
		}
		
		root.addChildNode(node);
		return SUCCESS;
	}
	
	public PmGroup getPmGroup() {
		return pmGroup;
	}
	public void setPmGroup(PmGroup pmGroup) {
		this.pmGroup = pmGroup;
	}
	public PmGroupService getPmGroupService() {
		return pmGroupService;
	}
	public void setPmGroupService(PmGroupService pmGroupService) {
		this.pmGroupService = pmGroupService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public void setPmGroupMember(PmGroupMember pmGroupMember) {
		this.pmGroupMember = pmGroupMember;
	}
	public PmGroupMember getPmGroupMember() {
		return pmGroupMember;
	}
	public void setPmGroupMemberService(PmGroupMemberService pmGroupMemberService) {
		this.pmGroupMemberService = pmGroupMemberService;
	}
	public PmGroupMemberService getPmGroupMemberService() {
		return pmGroupMemberService;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public File getUpload() {
		return upload;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}

	

	public void setSpecialtys(Collection<PmSpecialty> specialtys) {
		this.specialtys = specialtys;
	}

	public Collection<PmSpecialty> getSpecialtys() {
		return specialtys;
	}

	public void setPmSpecialtyService(PmSpecialtyService pmSpecialtyService) {
		this.pmSpecialtyService = pmSpecialtyService;
	}

	public PmSpecialtyService getPmSpecialtyService() {
		return pmSpecialtyService;
	}

	public void setCostWork(String costWork) {
		this.costWork = costWork;
	}

	public String getCostWork() {
		return costWork;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getRoot() {
		return root;
	}


	
	@SuppressWarnings("unchecked")
	private Collection<PmGroupMember> getStorMembers()
	{
		return (Collection<PmGroupMember>)this.getStorage().get(key);
	}

	public void setMembers(Collection<PmGroupMember> members) {
		this.members = members;
	}

	public Collection<PmGroupMember> getMembers() {
		return members;
	}


}
